package com.liug.droidsmall2.view;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import com.liug.droidsmall2.R;

/**
 * Created by LG on 2018/8/9.
 */

public class MenuUtils extends View {

    private float cx_cril;
    private float cy_cril;
    float radius_out = 650;                //给定半径
    float radius_inner = 450;                //给定半径
    Context context;
    private GestureDetector gestureDetector;
    private RectF rectF;

    /**
     * 创建一个继承View的class
     * View一共有四个构造器 这里先说两个
     * 第一个构造器的参数就是context,这是在Activity实例化的时候所需的,比如Button button = new Button(context);
     * 第二个构造器有两个参数 第一个同样是context 第二个参数AttributeSet放入了一些属性,这是我们在写XML文件时用到的比如
     * android:layout_width="match_parent"
     * android:layout_height="match_parent"如果不写函数的话是无法通过XML添加View
     */
    public MenuUtils(Context context) {
        super(context);
        moveView();
        this.context = context;
        inti();
    }

    public MenuUtils(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        moveView();
        inti();
    }

    private void inti() {
        gestureDetector = new GestureDetector(new GestureDetector.OnGestureListener() {
            @Override
            public boolean onDown(MotionEvent e) {
                return true;
            }

            @Override
            public void onShowPress(MotionEvent motionEvent) {

            }

            @Override
            public boolean onSingleTapUp(MotionEvent e) {

                if(e.getX()>rectF.left && e.getX()<rectF.right && e.getY()>rectF.top && e.getY()<rectF.bottom){

                    Toast.makeText(context, "dsfsd", Toast.LENGTH_SHORT).show();

                }

                return true;
            }

            @Override
            public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
                return false;
            }

            @Override
            public void onLongPress(MotionEvent motionEvent) {

            }

            @Override
            public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
                return false;
            }
        });

    }
    private int a = 0;
    private int b = -0;
    private int c = 600;
    private boolean isRun;
    //重写onDraw方法
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);


        canvas.drawColor(Color.parseColor("#00000000"));      //设置canvas的背景色

        //给定圆心的的坐标
        cx_cril = 0;
        cy_cril = getMeasuredHeight() / 2;

//        画外圆
        drawoutCril(canvas);

//        画内圆
        drawinnerCril(canvas);

//        画中间参数
        drawLinear(canvas);

//        画外层菜单
        drawLinearMenu(canvas);

//        startAnim(canvas);




        dra(canvas);


    }

    private void dra(Canvas canvas) {
        Paint paint1 = new Paint();
        paint1.setColor(Color.WHITE);//实例化一个Paint对象
        canvas.drawArc(100,0,0,0,100,30, true, paint1);


    }


    public void moveView(){
        if(isRun == false ){
            isRun  = true;
        } else if(isRun==true){
            isRun = false;
        }
        new Thread(moveThread).start();
    }
    private final Runnable moveThread = new Runnable() {

        @Override
        public void run() {
            a = 0;
            b = 0;
            c = 600;
            while(isRun = true){
                a+=5;
                b+=5;
                c+=5;
                if(c>=1200){
                    c=-600;
                }
                if(b>=1200){
                    b=-600;
                }
                if(a>=1200){
                    a = -600;
                }
                try {
                    Thread.sleep(30);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                postInvalidate();
            }
        }
    };





    /**
     * 画外层菜单
     *
     * @param canvas
     */
    private void drawLinearMenu(Canvas canvas) {

    }

    /**
     * 画中间参数
     *
     * @param canvas
     */
    private void drawLinear(Canvas canvas) {

        Paint paint1 = new Paint();       //实例化一个Paint对象

        paint1.setColor(Color.parseColor("#ffffff"));
        paint1.setTextSize(45);
        canvas.drawText("体验得分", cx_cril + 40, cy_cril - 145, paint1);

        paint1.setColor(Color.parseColor("#ffffff"));
        paint1.setTextSize(130);

        canvas.drawText("100", cx_cril + 40, cy_cril - 30, paint1);

        paint1.setColor(Color.parseColor("#ffffff"));
        canvas.drawLine(cx_cril + 40, cy_cril, cx_cril + 300, cy_cril, paint1);

        paint1.setColor(Color.parseColor("#ffffff"));
        paint1.setTextSize(45);
        canvas.drawText("你好,雅安", cx_cril + 80, cy_cril + 70, paint1);

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.sadc);
        canvas.drawBitmap(bitmap, cx_cril + 50, cy_cril + 100, paint1);

        int height = bitmap.getHeight()/2;
        int width = bitmap.getWidth()/2;

        canvas.drawBitmap(bitmap, radius_out/3-width+b,cy_cril-(float) (radius_out * (Math.sqrt(3) / 2)+height+b), paint1);

        canvas.drawBitmap(bitmap, radius_out/3-width+b,cy_cril+(float) (radius_out * (Math.sqrt(3) / 2)-height+b), paint1);

    /*    canvas.drawBitmap(bitmap, 2*(radius_out/3)-width,cy_cril-radius_out/2-height, paint1);
        canvas.drawBitmap(bitmap, 2*(radius_out/3)-width,cy_cril+radius_out/2-height, paint1);*/

        canvas.drawBitmap(bitmap, 2*(radius_out/3)+b,cy_cril-radius_out/2-height+b, paint1);
        canvas.drawBitmap(bitmap, 2*(radius_out/3)+b,cy_cril+radius_out/2-height+b, paint1);

        canvas.drawBitmap(bitmap, radius_out-60-width+b,cy_cril-height+b, paint1);

        rectF = new RectF(100,100,0,0);
    }


    private void drawinnerCril(Canvas canvas) {
        Paint paint1 = new Paint();       //实例化一个Paint对象
        paint1.setColor(Color.parseColor("#55ffffff"));
        canvas.drawCircle(cx_cril, cy_cril, radius_out, paint1);
    }


    private void drawoutCril(Canvas canvas) {
        Paint paint = new Paint();       //实例化一个Paint对象
        paint.setColor(Color.parseColor("#33ffffff"));
        canvas.drawCircle(cx_cril, cy_cril, radius_inner-b, paint);


    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        gestureDetector.onTouchEvent(event);
        return true;
    }


    private void startAnim(Canvas canvas){
        ObjectAnimator animator = ObjectAnimator.ofFloat(this,"translationX",0,500);
        animator.setRepeatMode(ObjectAnimator.RESTART);
        animator.setRepeatCount(ObjectAnimator.INFINITE);
        animator.setDuration(5000);
        animator.start();
    }


}
